package com.yiji.fintech.musfinance.facadeimpl.notify;

import com.acooly.core.utils.enums.ResultStatus;
import com.alibaba.dubbo.config.annotation.Service;
import com.yiji.fintech.musfinance.center.notify.api.TransResultNotifyFacade;
import com.yiji.fintech.musfinance.center.notify.order.base.NotifyOrderBase;
import com.yiji.fintech.musfinance.center.notify.result.NotifyResult;
import com.yiji.fintech.musfinance.common.context.SpringApplicationContext;
import com.yiji.fintech.musfinance.common.factory.strategy.StrategyOrderLoadFactory;
import com.yiji.fintech.musfinance.common.strategy.context.NotifyContext;
import com.yiji.fintech.musfinance.common.strategy.notify.NotifyStrategyBase;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 交易异步通知
 * Created by ouwen@yiji.com} on 2017/9/25.
 */
@Service(version = "1.0")
public class TransResultNotifyFacadeImpl implements TransResultNotifyFacade {
    @Autowired
    private StrategyOrderLoadFactory factory;

    @Override
    public NotifyResult notify(NotifyOrderBase notifyOrder) {
        Map<Class,Class> relationship = factory.getStrategyOrderMap();//异步通知Order与异步通知处理策略关系
        Class strategyClass = relationship.get(notifyOrder.getClass());
        NotifyStrategyBase notifyStrategy = (NotifyStrategyBase)SpringApplicationContext.getBeanByClass(strategyClass);
        NotifyContext context = new NotifyContext(notifyStrategy);
        NotifyResult result = new NotifyResult();
        result.setStatus(ResultStatus.success);
        result.setCode(ResultStatus.success.getCode());
        result.setDetail(ResultStatus.success.getMessage());
        return context.doNofity(notifyOrder,result);
    }
}
